package com.itheima.bank.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.bank.domain.Account;
import com.itheima.bank.domain.Result;
import com.itheima.bank.domain.Trade;
import com.itheima.bank.service.BankService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@RestController
public class BankController {
    @Reference
    BankService bankService;

    /**
     * 验证用户登陆
     * 登录成功后实现页面跳转
     */
    @PostMapping("/login")
    public Result userLogin(Account account, HttpSession session) {
        Account _account = bankService.userLogin(account);
        Result result = Result.builder().code(1001).msg("用户名或密码错误").build();
        if (_account != null) {
            // 登录成功 调整页面 保存信息到Session
            session.setAttribute("account", _account);
            result.setMsg("登录成功");
            result.setCode(1000);
        }
        return result;
    }

    /**
     * 查询已登录用户名称
     */
    @GetMapping("/findAccountId")
    private Result selectAccountid(HttpSession session) {
        Account account = (Account) session.getAttribute("account");
        Result result = Result.builder().code(1002).msg("用户尚未登录").build();
        if (account != null) {
            result.setCode(1000);
            result.setMsg(account.getAccountid());
        }
        return result;
    }

    /**
     * 用户登陆成功后查询余额
     */
    @GetMapping("/selectRemaining")
    private Result selectRemaining(HttpSession session) {
        Account account = (Account) session.getAttribute("account");
        Result result = Result.builder().code(1002).msg("你都没有登陆你查什么余额").build();
        if (account != null) {
            Double remaining = bankService.selectRemaining(account.getAccountid());
            result.setCode(1000);
            result.setMsg(remaining.toString());
        }
        return result;
    }

    /**
     * 处理用户转账操作
     */
    @PostMapping("/transMoney")
    private Result transMoney(HttpSession session, String to_account, Double t_money) {
        Account account = (Account) session.getAttribute("account");
        Result result = Result.builder().code(1002).msg("没有登陆不能转账啊").build();
        // 先判断用户是否登陆
        if (account != null) {
            String transaction = bankService.accountTransaction(account.getAccountid(), to_account, t_money);
            result.setCode(1000);
            result.setMsg(transaction);
        }

        return result;
    }

    /**
     * 用户存款操作
     */
    @PostMapping("/saveMoney")
    private Result saveMoney(HttpSession session, Double save_money) {
        Account account = (Account) session.getAttribute("account");
        Result result = Result.builder().code(1002).msg("没有登陆不能存钱啊").build();
        if (null == save_money || save_money <= 0) {
            result.setCode(1002);
            result.setMsg("你不要瞎填哦");
            return result;
        }
        // 先判断用户是否登陆
        if (account != null) {
            String transaction = bankService.savemoney(account.getAccountid(), save_money);
            result.setCode(1000);
            result.setMsg(transaction);
        }

        return result;
    }

    /**
     * 用户取钱操作
     */
    @PostMapping("/takeMoney")
    private Result takeMoney(HttpSession session, Double take_money) {
        Account account = (Account) session.getAttribute("account");
        Result result = Result.builder().code(1002).msg("没有登陆不能取钱啊").build();
        if (null == take_money || take_money <= 0) {
            result.setCode(1002);
            result.setMsg("你不要瞎填哦");
            return result;
        }
        // 先判断用户是否登陆
        if (account != null) {
            String transaction = bankService.takemoney(account.getAccountid(), take_money);
            result.setCode(1000);
            result.setMsg(transaction);
        }

        return result;
    }

    /**
     * 查询记录操作
     */
    @PostMapping("/selectTrade")
    private Result selectTrade(HttpSession session, String startTime, String endTime) throws Exception {
        Account account = (Account) session.getAttribute("account");
        Result result = Result.builder().code(1002).msg("没有登陆不能查询啊").build();

        if ("".equals(startTime) || "".equals(endTime)) {
            result.setCode(1024);
            result.setMsg("查询日期不能为空");

            return result;
        }
        // 判断日期格式是否正确
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date _startDate = sdf.parse(startTime);
        Date _endDate = sdf.parse(endTime);
        Calendar cc = Calendar.getInstance();
        cc.setTime(new Date());
        cc.add(Calendar.MONTH, -1);
        Date _bfDate = cc.getTime();
        // 结束日期应大于开始日期
        if (_endDate.before(_startDate)) {
            result.setCode(1024);
            result.setMsg("开始日期不能大于结束日期");
            return result;
        }
        // 查询日期不能超过一个月
        if (_bfDate.after(_startDate)) {
            result.setCode(1024);
            result.setMsg("只能查询近一个月的记录噢");
            return result;
        }
        // 查询日期不能超过当前日期
        if (new Date().before(_endDate)) {
            result.setCode(1024);
            result.setMsg("查询日期不能超过当前日期噢");
            return result;
        }
        // 先判断用户是否登陆
        if (account != null) {
            // 调用业务层
            List<Trade> tTrades = bankService.selectTrade(account.getAccountid(), startTime, endTime);
            result.setCode(999);
            result.setMsg("查询成功");
            result.setList(tTrades);
        }
        return result;
    }
}
